#include "m0wig.h"
#include "ui_m0wig.h"
#include "mqtthandler.h"
#include "globalval.h"

#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>


struct DeviceData {
    int key;
    QString name;
    QVariant data; // 使用QVariant来存储不同类型的数据(false/true/数值)
};


M0Wig::M0Wig(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::M0Wig)
{
    ui->setupUi(this);

    led1_state=false;
    led2_state=false;
    fan1_state=false;
    wat1_state=false;


    led1_btn=new mybtn(this);
    led2_btn=new mybtn(this);
    fan_btn=new mybtn(this);
    wat_btn=new mybtn(this);

    led1_btn->move(70,100);
    led2_btn->move(430,100);
    fan_btn->move(70,330);
    wat_btn->move(430,330);


    led1_btn->setText("照明1");
    led2_btn->setText("照明2");
    fan_btn->setText("通风");
    wat_btn->setText("浇水");

    connect(led1_btn,&mybtn::clicked,this,M0Wig::ledbtn1_slots);
    connect(led2_btn,&mybtn::clicked,this,M0Wig::ledbtn2_slots);
    connect(fan_btn,&mybtn::clicked,this,M0Wig::fanbtn_slots);
    connect(wat_btn,&mybtn::clicked,this,M0Wig::watbtn_slots);
}

M0Wig::~M0Wig()
{
    delete ui;
}

void M0Wig::ledbtn1_slots()
{

    QMQTT::Message msg;
    msg.setTopic(TOPIC_CTL_DOWN);
    if(led1_state == false)
    {
        QByteArray payload = "{\"key\":1,\"name\":\"control\",\"data\":{\"key\":1,\"name\":\"lamp\",\"data\":\"true\"}}";
        msg.setPayload(payload);
        GlobalVal::mqtt->publish(msg);
        led1_btn->setStyleSheet("background-color:#4CAF50;color:white;");
        led1_btn->setText("已开灯");
        led1_state =true;
    }else
    {
        QByteArray payload =  "{\"key\":1,\"name\":\"control\",\"data\":{\"key\":1,\"name\":\"lamp\",\"data\":\"false\"}}";
        msg.setPayload(payload);
        GlobalVal::mqtt->publish(msg);
        led1_btn->setStyleSheet(" ");
        led1_btn->setText("照明1");
        led1_state = false;
    }
}

void M0Wig::ledbtn2_slots()
{
    QMQTT::Message msg;
    msg.setTopic(TOPIC_CTL_DOWN);
    if(led2_state == false)
    {
        QByteArray payload =  "{\"key\":7,\"name\":\"control\",\"data\":{\"key\":1,\"name\":\"lamp2\",\"data\":\"true\"}}";
        msg.setPayload(payload);
        GlobalVal::mqtt->publish(msg);
        led2_btn->setStyleSheet("background-color:#4CAF50;color:white;");
        led2_btn->setText("已开灯");
        led2_state =true;
    }else
    {
        QByteArray payload = "{\"key\":7,\"name\":\"control\",\"data\":{\"key\":1,\"name\":\"lamp2\",\"data\":\"false\"}}";
        msg.setPayload(payload);
        GlobalVal::mqtt->publish(msg);
        led2_btn->setStyleSheet(" ");
        led2_btn->setText("照明2");
        led2_state = false;
    }
}

void M0Wig::fanbtn_slots()
{
    QMQTT::Message msg;
    msg.setTopic(TOPIC_CTL_DOWN);
    if( fan1_state == false)
    {
        QByteArray payload =  "{\"key\":2,\"name\":\"control\",\"data\":{\"key\":1,\"name\":\"fan\",\"data\":\"true\"}}";
        msg.setPayload(payload);
        GlobalVal::mqtt->publish(msg);
        fan_btn->setStyleSheet("background-color:#4CAF50;color:white;");
        fan_btn->setText("通风开");
        fan1_state =true;
    }else
    {
        QByteArray payload = "{\"key\":2,\"name\":\"control\",\"data\":{\"key\":1,\"name\":\"fan\",\"data\":\"false\"}}";
        msg.setPayload(payload);
        GlobalVal::mqtt->publish(msg);
        fan_btn->setStyleSheet( " ");
        fan_btn->setText("通风");
        fan1_state = false;
    }

}

void M0Wig::watbtn_slots()
{
    QMQTT::Message msg;
    msg.setTopic(TOPIC_CTL_DOWN);
    if( wat1_state == false)
    {
        QByteArray payload = "{\"key\":3,\"name\":\"control\",\"data\":{\"key\":1,\"name\":\"irrigated\",\"data\":\"true\"}}";
        msg.setPayload(payload);
        GlobalVal::mqtt->publish(msg);
        wat_btn->setStyleSheet("background-color:#4CAF50;color:white;");
        wat_btn->setText("浇水开");
        wat1_state =true;
    }else
    {
        QByteArray payload = "{\"key\":3,\"name\":\"control\",\"data\":{\"key\":1,\"name\":\"irrigated\",\"data\":\"false\"}}";
        msg.setPayload(payload);
        GlobalVal::mqtt->publish(msg);
        wat_btn->setStyleSheet(" ");
        wat_btn->setText("浇水");
        wat1_state = false;
    }
}

void M0Wig::m0msg(const QByteArray &msg_t)
{
    QJsonDocument doc = QJsonDocument::fromJson(msg_t);


    if (!doc.isObject()) {
        qDebug() << "Document is not an object";
        return;
    }

    QJsonObject obj = doc.object();
    if (!obj.contains("data") || !obj["data"].isArray()) {
        qDebug() << "Invalid JSON format - missing 'data' array";
        return;
    }

    QJsonArray dataArray = obj["data"].toArray();
    QList<DeviceData> deviceList;

    QJsonArray::const_iterator it;
    for (it = dataArray.constBegin(); it != dataArray.constEnd(); ++it) {
        const QJsonValue &value = *it;

        QJsonObject obj = value.toObject();
        DeviceData device;

        device.key = obj["key"].toInt();
        device.name = obj["name"].toString();

        // 根据数据类型处理data字段
        if (obj["data"].isBool()) {
            device.data = obj["data"].toBool();
        } else if (obj["data"].isDouble()) {
            device.data = obj["data"].toDouble();
        } else if (obj["data"].isString()) {
            // 处理字符串形式的布尔值
            QString strData = obj["data"].toString().toLower();
            if (strData == "true" || strData == "false") {
                device.data = (strData == "true");
            } else {
                device.data = strData;
            }
        }

         deviceList.append(device);
        if(device.name == "lamp")
        {
            if(device.data == true){
                led1_btn->setStyleSheet("background-color:#4CAF50;color:white;");
                led1_btn->setText("已开灯");
                led1_state =true;
            }else{
                led1_btn->setStyleSheet(" ");
                led1_btn->setText("照明1");
                led1_state = false;
            }
        }
        if(device.name == "lamp2")
        {
            if(device.data == true){
                led2_btn->setStyleSheet("background-color:#4CAF50;color:white;");
                led2_btn->setText("已开灯");
                led2_state =true;
            }else{
                led2_btn->setStyleSheet(" ");
                led2_btn->setText("照明2");
                led2_state = false;
            }
        }
        if(device.name == "fan")
        {
            if(device.data == true){
                fan_btn->setStyleSheet("background-color:#4CAF50;color:white;");
                fan_btn->setText("通风开");
                fan1_state =true;
            }else{
                fan_btn->setStyleSheet(" ");
                fan_btn->setText("通风");
                fan1_state = false;
            }
        }
        if(device.name == "irrigated")
        {
            if(device.data == true){
                wat_btn->setStyleSheet("background-color:#4CAF50;color:white;");
                wat_btn->setText("浇水开");
                wat1_state =true;
            }else{
                wat_btn->setStyleSheet(" ");
                wat_btn->setText("浇水");
                wat1_state = false;
            }
        }

    }


}
